﻿using System;

namespace DingTalk.Common
{
    public class CreateIdHelper
    {
        /// <summary>  
        /// 根据GUID获取16位的唯一字符串 并转成大写  (使用在 优惠码中)
        /// </summary>  
        /// <returns></returns>  
        public static string GuidTo16String()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
                i *= ((int)b + 1);
            return string.Format("{0:x}", i - DateTime.Now.Ticks).ToUpper();
        }

        /// <summary>  
        /// 根据GUID获取19位的唯一数字序列  
        /// </summary>  
        /// <returns></returns>  
        public static long GuidToLongID()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            return BitConverter.ToInt64(buffer, 0);
        }

        /// <summary>
        /// 获取8位唯一码
        /// </summary>
        /// <returns></returns>
        public static string GenUniqueString()
        {
            string key = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            int len = 8;
            char[] rtn = new char[len];
            Guid gid = Guid.NewGuid();
            var ba = gid.ToByteArray();

            string rtnkey = "";
            for (var i = 0; i < len; i++)
            {
                rtn[i] = key[((ba[i] + ba[len + i]) % 35)];

                rtnkey += rtn[i];
            }
            return rtnkey;
        }
    }
}
